package ingest.blog.icwsm;


import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.SequenceInputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Enumeration;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class ICWSM2009Ingester {

	private static String root = "/local/corpora/blogs/";


	public static ArrayList<File> findAllFilesInDir(File dir) {
		ArrayList<File> files = new ArrayList<File>();
		for (File file : dir.listFiles()) {
			if (file.isDirectory()) {
				files.addAll(findAllFilesInDir(file));
			} else {
				if (file.getAbsoluteFile().toString().endsWith(".xml"))
					files.add(file);
			}
		}
		return files;

	}

	public void ingest(boolean dropDatabase) {
		ArrayList<File> entries = findAllFilesInDir(new File(root));
		for (File f : entries)
			parse(f);
	}

	public ICWSM2009Ingester() {
//		 db = BlogDB.getInstance();
	}

	public static void main(String[] args) {
//		ICWSM2009Ingester ingester = new ICWSM2009Ingester();
//		ingester.ingest(true);

	}

	public void parse(File f) {
		try {
			Enumeration<InputStream> streams = Collections.enumeration(Arrays
					.asList(new InputStream[] {
							new ByteArrayInputStream("<item>".getBytes()),
							new FileInputStream(f),
							new ByteArrayInputStream("</item>".getBytes()), }));

			SequenceInputStream seqStream = new SequenceInputStream(streams);
			DocumentBuilderFactory dbFactory = DocumentBuilderFactory
					.newInstance();
			DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
			Document doc = dBuilder.parse(seqStream);
			NodeList items = doc.getElementsByTagName("item");

			for (int i = 0; i < items.getLength(); i++) {

				Node item = items.item(i);
				NodeList features = item.getChildNodes();
//				Blog blogEntry = new Blog();

				for (int j = 0; j < features.getLength(); j++) {
//					blogEntry.processNode(features.item(j));

				}
//				db.saveEntry(blogEntry);

			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
}

